package com.example.tellwin.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.mine.bean.AttentionTeacherBean;
import com.example.tellwin.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/12
 * @Describe ：
 **/
public class TeacherAdapter extends RecyclerView.Adapter<TeacherAdapter.TeacherViewHolder> {

    Context context;
    private List<AttentionTeacherBean>teacherBeans =new ArrayList<>();
    private OnTeacherItemClickListener onTeacherItemClickListener;

    public void setTeacherBeans(List<AttentionTeacherBean> teacherBeans) {
        this.teacherBeans = teacherBeans;
        notifyDataSetChanged();
    }

    public void setOnTeacherItemClickListener(OnTeacherItemClickListener onTeacherItemClickListener) {
        this.onTeacherItemClickListener = onTeacherItemClickListener;
    }

    @NonNull
    @Override
    public TeacherViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new TeacherViewHolder(LayoutInflater.from(context).inflate(R.layout.item_teacher, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull TeacherViewHolder holder, int position) {
        AttentionTeacherBean teacherBean = teacherBeans.get(position);
        GlideUtils.loadHeadImage(context , teacherBean.getHeadPic() , holder.teacherHeadIv);
        holder.teacherNameTv.setText(teacherBean.getRealName());
        holder.teacherSchoolTv.setText(teacherBean.getSchool());
        holder.teacherSubjectTv.setText(teacherBean.getSubjectName());
        holder.teacherRankTv.setText(teacherBean.getQualificationsName());
        holder.itemRl.setOnClickListener(v -> {
            if(onTeacherItemClickListener!=null){
                onTeacherItemClickListener.onItemClick(position,teacherBean);
            }
        });
        holder.cancelAttentionTv.setOnClickListener(v -> {
            if(onTeacherItemClickListener!=null){
                onTeacherItemClickListener.onItemCancelAttentionClick(position,teacherBean);
            }

        });
    }

    @Override
    public int getItemCount() {
        return teacherBeans.size();
    }


    class TeacherViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemRl;
        ImageView teacherHeadIv;
        TextView teacherNameTv;
        TextView teacherSchoolTv;
        TextView teacherSubjectTv;
        TextView cancelAttentionTv;
        TextView teacherRankTv;

        public TeacherViewHolder(@NonNull View itemView) {
            super(itemView);
            itemRl = itemView.findViewById(R.id.item_rl);
            teacherHeadIv = (ImageView) itemView.findViewById(R.id.teacher_head_iv);
            teacherNameTv = (TextView) itemView.findViewById(R.id.teacher_name_tv);
            teacherSchoolTv = (TextView) itemView.findViewById(R.id.teacher_school_tv);
            teacherSubjectTv = (TextView) itemView.findViewById(R.id.teacher_subject_tv);
            cancelAttentionTv = (TextView) itemView.findViewById(R.id.cancel_attention_tv);
            teacherRankTv = itemView.findViewById(R.id.teacher_rank_tv);
        }
    }

    public interface OnTeacherItemClickListener{
        void onItemClick(int position ,AttentionTeacherBean teacherBean);
        void onItemCancelAttentionClick(int position ,AttentionTeacherBean teacherBean);
    }
}
